from django.test import TestCase
from django.conf import settings
import pymysql
import random
import string


def random_name():
    num = random.randint(4, 10)
    return "".join([random.choice(string.ascii_lowercase) for i in range(num)])


def insert():
    my_connect = pymysql.connect(host='127.0.0.1', user='root', password='root', database='django_db', port=3306,
                                 charset='utf8')
    my_cursor = my_connect.cursor()

    sql = "INSERT INTO buyer(id,name,password) VALUES (1, 'wangdachui', 'e10adc3949ba59abbe56e057f20f883e')"
    my_cursor.execute(sql)

    sql = "INSERT INTO reveiver(id,name,phone,postcode,address,isdefault,buyer_id) VALUES (1, '小强', '11111111111','111111','广州市',1,1)"
    my_cursor.execute(sql)

    sql = "INSERT INTO seller(id,name,password) VALUES (1, 'zhangsan', 'e10adc3949ba59abbe56e057f20f883e')"
    my_cursor.execute(sql)

    sql = "INSERT INTO store(id,name,seller_id) VALUES (1, '杂货铺',1)"
    my_cursor.execute(sql)

    sql = 'INSERT INTO goodstype(id,name,description,picture) values(%s,%s,%s,%s)'
    my_cursor.execute(sql, [1, '新鲜水果', '新鲜水果', 'banner01.jpg'])
    my_cursor.execute(sql, [2, '海鲜水产', '海鲜水产', 'banner02.jpg'])
    my_cursor.execute(sql, [3, '猪牛羊肉', '猪牛羊肉', 'banner03.jpg'])
    my_cursor.execute(sql, [4, '禽类蛋品', '禽类蛋品', 'banner04.jpg'])
    my_cursor.execute(sql, [5, '新鲜蔬菜', '新鲜蔬菜', 'banner05.jpg'])
    my_cursor.execute(sql, [6, '速冻食品', '速冻食品', 'banner06.jpg'])

    for i in range(1, 7):
        sql = 'INSERT INTO store_goodstypes(id,store_id,goodstype_id) values(%s,%s,%s)'
        my_cursor.execute(sql, [i, 1, i])

    for i in range(1, 7):
        for j in range(100):
            print(i,j)
            sql = 'INSERT INTO goods' \
                  '(name,price,picture,number,description,detail,producttime,shelfife,store_id,goodstype_id,unite,sale) ' \
                  'values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
            my_cursor.execute(sql, [
                random_name(), random.randint(1, 100), 'goods02.jpg', random.randint(1, 10000), '这里是描述','这里是详情',
                '2019-10-10', random.randint(1, 10000), 1, i, '500g', random.randint(1, 10000)])

    my_connect.commit()
    my_cursor.close()
    my_connect.close()
    print('OK')

def my_send_email():
    """发邮件"""
    # 导入包
    import smtplib
    from email.mime.text import MIMEText

    # 邮件信息-发件人
    sender = "jack_dawson_email@163.com"
    # 邮件信息-收件人
    receiver = "gebidaxiaowang@126.com"
    # 邮件信息-授权码
    password = "ymm123"
    # 邮件信息-主题
    subject = "发邮件0421"
    # 邮件信息-内容
    content = "你好，<a href='http://127.0.0.1:8000/'>单击访问<strong>每日生鲜</strong>首页</a>。"

    # 创建邮件文本对象（内容，类型，编码）
    mime_text = MIMEText(content, "html", "utf-8")
    # 赋值属性
    mime_text["Subject"] = subject
    mime_text["From"] = sender
    mime_text["To"] = receiver

    # 创建smtp对象，这里的163和发件人163对应
    my_smtp = smtplib.SMTP_SSL("smtp.163.com", 994)
    # 登录，账号错误会抛异常
    my_smtp.login(sender, password)
    # 发送邮件(发件人，收件人集合，内容)，返回值是1表示发送成功，其他失败
    my_smtp.sendmail(sender, [receiver], mime_text.as_string())
    # 关闭
    my_smtp.quit()

    print("发送成功")


def my_send_email_many():
    """发邮件-发个多人"""
    # 导入包
    import smtplib
    from email.mime.text import MIMEText

    # 邮件信息-发件人
    sender = "jack_dawson_email@163.com"
    # 邮件信息-收件人
    receiver1 = "gebidaxiaowang@126.com"
    receiver2 = "278455900@qq.com"
    # 邮件信息-授权码
    password = "ymm123"
    # 邮件信息-主题
    subject = "发邮件0421"
    # 邮件信息-内容
    content = "你好，<a href='http://127.0.0.1:8000/'>单击访问<strong>每日生鲜</strong>首页</a>。"

    # 创建邮件文本对象（内容，类型，编码）
    mime_text = MIMEText(content, "html", "utf-8")
    # 赋值属性
    mime_text["Subject"] = subject
    mime_text["From"] = sender
    mime_text["To"] = ",".join([receiver1,receiver2])#多个收件人使用逗号隔开

    # 创建smtp对象
    my_smtp = smtplib.SMTP_SSL("smtp.163.com", 994)
    # 登录，账号错误会抛异常
    my_smtp.login(sender, password)
    # 发送邮件(发件人，收件人集合，内容)
    my_smtp.sendmail(sender, [receiver1,receiver2], mime_text.as_string())
    # 关闭
    my_smtp.quit()

    print("发送成功")

if __name__ == '__main__':
    insert()